package com.yuan.yblog.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuan.yblog.common.api.CommonResult;
import com.yuan.yblog.portal.aop.annotation.SlidingWindowLimiter;
import com.yuan.yblog.portal.enums.LimitType;
import com.yuan.yblog.portal.pojo.VO.UserMessageVO;
import com.yuan.yblog.portal.service.UserMessageService;
import com.yuan.yblog.portal.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (UserMessage)表控制层
 * @since 2023-12-03 14:21:18
 */
@RestController
@RequestMapping("/user/message")
@Tag(name = "用户消息（message）接口")
public class UserMessageController {
    @Autowired
    UserMessageService userMessageService;
    @Autowired
    UserService userService;

    @SlidingWindowLimiter(key = "user_message", windowSize = 120, windowTimeRange = 60, limitType = LimitType.IP)
    @Operation(summary = "分页当前用户收到的消息")
    @GetMapping("/list")
    public CommonResult<IPage<UserMessageVO>> queryUserMessage(
            @Parameter(description = "是否已读") @RequestParam(required = false) Boolean isRead,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNumber,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Long pageSize,
            @Parameter(description = "排序字段") @RequestParam(required = false) String sortKey,
            @Parameter(description = "是否降序") @RequestParam(required = false) Boolean isDesc) {
        IPage<UserMessageVO> results = userMessageService.list(isRead, pageNumber, pageSize, sortKey, isDesc);
        return CommonResult.success(results);
    }

    @Operation(summary = "用户【阅读】消息")
    @PutMapping("/read")
    public CommonResult<Integer> ReadMessage(@Parameter(description = "消息id集合") @RequestParam List<Long> ids) {
        int count = userMessageService.ReadMessage(ids);
        return CommonResult.success(count, "生效数目：" + count);
    }

    @Operation(summary = "用户【删除】消息")
    @DeleteMapping("/remove")
    public CommonResult<Integer> delete(@Parameter(description = "消息id集合") @RequestParam List<Long> ids) {
        int count = userMessageService.deleteByIds(ids);
        return CommonResult.success(count, "生效数目："+count);
    }

    @Operation(summary = "获取当前用户【未读】消息数")
    @GetMapping("/unreadCount")
    public CommonResult<Long> unreadCount(){
        Long count = userMessageService.getUnreadCount();
        return CommonResult.success(count,"未读消息数："+count);
    }
}

